package com.itheima.web;

import com.alibaba.druid.sql.dialect.odps.ast.OdpsAddStatisticStatement;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Orders;
import com.itheima.bean.User;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: LiuBanXian
 * @Date: 2022/9/4  15:57
 * @description:
 */
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    /**
     * 分页
     * @param pageParam
     * @return
     */
    @GetMapping("/order/page")
    public R findPage(PageParam pageParam) {
        IPage<Orders> page = orderService.findPage(pageParam);
        return R.success(page);

    }
    /**
     * 提交订单
     * @param orders
     * @return
     */
    @PostMapping("/order/submit")
    public R add(@RequestBody Orders orders , HttpSession session){

        //获取用户对象
        User user = (User) session.getAttribute("user");

        int row = orderService.add(orders , user);
        if(row > 0 ){
            return R.success("提交订单成功！");
        }
        return R.error("提交订单失败！");
    }

    @PutMapping("/order")
    public R update(@RequestBody Orders orders) {
        //调用service
        int row = orderService.update(orders);

        //返回结果
        if (row > 0) {
            return R.success("订单状态修改成功");
        }
        return R.error("订单状态修改失败");
    }

}
